<!-- 仪表盘 -->
<template>
  <SearchTable :query="data.query" :list="data.list" :total="data.total" :showPanel="true" @changeSize="changeSize"
    @changePage="changePage" @search="getSearchList" @reset="onHideDetails(true)" @sort="sortTable" @changeTh="data.dialogVisible = true">
    <template #form>
      <Operator ref="OperatorRef" @query="queryItem" />
      <el-form-item v-if="!indexStore.is_general_agent">
        <el-button type="warning" @click="data.query.is_usdt_show = data.query.is_usdt_show?0:1,getSearchList()">{{ data.query.is_usdt_show?'取消换算':'换算USDT' }}</el-button>
      </el-form-item>
    </template>
    <template #button>
      <div v-loading="indexStore.loading" class="flexWrap">
        <div class="box-data margin20" v-for="(item, index) in data.operatorList" :key="index">
          <p class="box-title marginL20">
            <el-tooltip v-if="item.tips" effect="dark" placement="top">
              <template #content> {{item.tips}} </template>
              <el-icon class="ques-tips">
                <QuestionFilled />
              </el-icon>
            </el-tooltip>
            {{ item.label }}
          </p>
          <div class="flexStart">
            <div class="box-icon">
              <img :src="item.url" alt="">
            </div>
            <div class="width50">
              <div v-if="item.label == '今日热门'" class="game_name" style="height: 80px;overflow: auto;">
                <span v-for="(second, two) in item.value" :key="two" class="marginR5">{{ two + 1 }}.{{ second }}</span>
              </div>
              <div v-else class="textCenter">
                <p class="box-value marginT10 text-cut">{{ item.value }}</p>
                <p class="colorDark flex">
                  <img v-if="item.radio && item.radio > 0" src="@/assets/images/icon/up.png" alt="" />
                  <img v-else src="@/assets/images/icon/down.png" alt="" />
                  <span :class="colorClass(item.radio)">{{ item.radio }}%</span>
                </p>
              </div>
            </div>
          </div>
          <div class="flexBetween" style="padding: 0 28px;margin-top: -5px;">
            <div class="item_gamesDetails" @click="jumpLink(item.name, item.path)"
              style="width: 60px;font-size: 12px !important;">查看详情
            </div>
            <p class="box-yesterday" v-if="item.label != '今日热门'">
            <div class="text-cut" v-if="item.label == '今日输赢'" style="width: 140px;">历史输赢：
              <el-tooltip effect="dark" placement="top">
                <template #content> {{  item.otherValue }} </template>
                <span :class="colorClass(item.otherValue)">{{  item.otherValue }}</span>
              </el-tooltip>
              </div>
            <span v-if="item.label == '活跃商户'">总商户：<span class="box-value">{{ item.otherValue }}</span></span>
            <span v-if="item.label == '今日盈利率'">历史盈利率：<span :class="colorClass(item.otherValue)">{{ item.otherValue }}%</span></span>
            </p>
          </div>
        </div>
      </div>
    </template>
    <template #table>
      <template v-for="(item, index) in data.fields" :key="index">
        <el-table-column v-if="item.visible" :prop="item.prop" :label="item.label" :width="item.width"
          :sortable="item.sortable ? 'custom' : false">
          <template #header>
            <el-tooltip v-if="item.tips" effect="dark" placement="top">
              <template #content> {{item.tips}} </template>
              <el-icon class="ques-tips">
                <QuestionFilled />
              </el-icon>
            </el-tooltip>
            {{ item.label }}
          </template>
          <template v-if="item.prop == 'game_id'" #default="scope">
            <GameHeaderInfo :game_id="scope.row.game_id" />
          </template>
          <template v-if="item.prop == 'win_percent'" #default="scope">
            {{ scope.row.win_percent }}%
          </template>
          <template v-if="item.prop == 'rtp'" #default="scope">
            {{ scope.row.rtp }}%
          </template>
          <template v-if="item.prop == 'history_rtp'" #default="scope">
            {{ scope.row.history_rtp }}%
          </template>
        </el-table-column>
      </template>
      <el-table-column label="操作" width="90">
        <template #header>
          <el-tooltip effect="dark" content="点击查看游戏记录详情" placement="top">
            <el-icon class="ques-tips">
              <QuestionFilled />
            </el-icon>
          </el-tooltip>
          详情
        </template>
        <template #default="scope">
          <el-button :disabled="scope.row.status ? true : false" type="primary" v-permiss="'/system/destroyManager'"
            size="small" @click="jumpLink('gameRecord', '/player/gameRecord')">
            详情
          </el-button>
        </template>
      </el-table-column>
    </template>
  </SearchTable>

  <!-- 变更表头弹框 -->
  <tableHeader :dialogVisible="data.dialogVisible" :list="data.fields" @cancel="data.dialogVisible = false"
    @resetData="resetData" />
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted, onUpdated } from 'vue'
import tableHeader from '@/components/tableHeader/tableHeader.vue'
import SearchTable from '@/components/SearchTable/SearchTable.vue'
import Operator, { type QueryType } from '@/components/FormItem/Operator.vue'
import { getInstrument } from '@/api/instrument'
import GameHeaderInfo from '@/components/FormItem/GameHeaderInfo.vue'
import { sortObjectArray, selectToLabel, colorClass } from '@/utils/index'
import { getDateDay, setTimeValue } from '@/utils/time'
import { BigMinus, BigPercentNumber, RtpPercent, BigWan, EarningPercent, EarningPercentRadio } from '@/utils/math'

import { useStore } from '@/stores/index'
const indexStore = useStore()
import { useRouter } from 'vue-router'
const router = useRouter()
import { useI18n } from 'vue-i18n'
const { t } = useI18n()

import photo_one from "@/assets/images/icon/photo_one.jpg"
import photo_two from "@/assets/images/icon/photo_two.jpg"
import photo_three from "@/assets/images/icon/photo_three.jpg"
import photo_four from "@/assets/images/icon/photo_four.jpg"
import photo_five from "@/assets/images/icon/photo_five.jpg"
import photo_six from "@/assets/images/icon/photo_six.jpg"
import photo_seven from "@/assets/images/icon/photo_seven.jpg"
import photo_eight from "@/assets/images/icon/photo_eight.jpg"
import photo_nine from "@/assets/images/icon/photo_nine.jpg"
import photo_ten from "@/assets/images/icon/photo_ten.jpg"

const data = reactive({
  query: {
    page: 1,
    size: 50,
    agent_id: -1,
    operated_id: -1,
    currency_id: -1,
    is_usdt_show: 0,
    date: [getDateDay(0), getDateDay(0)]
  },
  list: [],
  total: 0,
  fields: [
    { label: '游戏名称', prop: 'game_id', width: '240', visible: true, tips: t('des_1') },
    { label: '登录人数', prop: 'in_game_num', width: '', visible: true, sortable: true, tips: t('des_2') },
    { label: '投注用户', prop: 'valid_user', width: '', visible: true, sortable: true, tips: t('des_3') },
    { label: '新手玩家', prop: 'first_play_count', width: '', visible: true, sortable: true, tips: '生涯第一次进入此游戏的玩家数量' },
    { label: '下注次数', prop: 'game_count', width: '', visible: true, sortable: true, tips:'玩家在游戏内进行有效投注行为的次数' },
    { label: '人数赢百分比', prop: 'win_percent', width: '', visible: true, tips: '今日此游戏的玩家盈利的人数占比，值越高，在此游戏赢的玩家数越多' },
    { label: 'RTP', prop: 'rtp', width: '', visible: true, tips: t('des_9') },
    { label: '历史RTP', prop: 'history_rtp', width: '', visible: true, tips: '此游戏的历史回报率值越高玩家盈利越多，值越低平台盈利越多' }
  ],
  dialogVisible: false,
  exportLoading: false,
  operatorList: [
    {
      label: '下注次数',
      value: 0,
      radio: 0,
      key: 'game_count',
      name: 'gameWinning',
      path: '/gameReport/gameWinning',
      url: photo_one,
      tips: '玩家今日的下注次数，点击可查看今日游戏输赢情况'
    },
    {
      label: '今日RTP',
      value: 0,
      radio: 0,
      key: 'rtp',
      name: 'gameWinning',
      path: '/gameReport/gameWinning',
      url: photo_two,
      tips: t('des_9')
    },
    {
      label: '今日热门',
      value: [],
      key: 'hot_games',
      name: 'gameRank',
      path: '/gameReport/gameRank',
      url: photo_three,
      tips: '今日玩家游玩游戏次数排名'
    },
    {
      label: '总用户数',
      value: 0,
      radio: 0,
      key: 'total_person',
      name: 'userStatistics',
      path: '/player/userStatistics',
      url: photo_four,
      tips: '商户的总玩家数量，可根据商户筛选查看每个商户情况'
    },
    {
      label: '实时人数',
      value: 0,
      radio: 0,
      key: 'online_person',
      name: 'playerAccount',
      path: '/player/playerAccount',
      url: photo_five,
      tips: '当前在线玩家数量'
    },
    {
      label: '投注用户',
      value: 0,
      radio: 0,
      key: 'valid_user',
      name: 'playerAccount',
      path: '/player/playerAccount',
      url: photo_six,
      tips: '今日有进行投注行为的玩家数量，去重'
    },
    {
      label: '注册用户',
      value: 0,
      radio: 0,
      key: 'new_user',
      name: 'playerAccount',
      path: '/player/playerAccount',
      url: photo_seven,
      tips: '今日新注册的用户数量'
    },
    {
      label: '活跃商户',
      value: 0,
      radio: 0,
      key: 'active_agents',
      otherValue: 0,
      name: 'merchantDaily',
      path: '/gameReport/merchantDaily',
      url: photo_eight,
      tips: '今日有玩家进行投注行为的商户数量'
    },
    {
      label: '今日输赢',
      value: 0,
      radio: 0,
      key: 'game_income',
      otherValue: 0,
      name: 'merchantDaily',
      path: '/gameReport/merchantDaily',
      url: photo_ten,
      tips: '今日游戏输赢情况，正为平台盈利，负为玩家盈利'
    },
    {
      label: '今日盈利率',
      value: 0,
      radio: 0,
      key: 'today_earning',
      otherValue: 0,
      name: 'merchantDaily',
      path: '/gameReport/merchantDaily',
      url: photo_nine,
      tips: '今日游戏的盈利率统计，值越高游戏盈利越多'
    },
  ]
})

const queryData = reactive({  
  queryItem:{
    page: 1,
    size: 50,
    agent_id: -1,
    operated_id: -1,
    currency_id: -1,
    is_usdt_show: 0,
    date: [getDateDay(0), getDateDay(0)]
  }
}) 

// 商户类别 + 商户
const queryItem = (item: QueryType) => {
  data.query.agent_id = item.agent_id
  data.query.operated_id = item.operated_id
}

onMounted(async () => {
  data.query = indexStore.setQueryCache(data.query,'get')
  OperatorRef.value.setBackQuery(data.query)
  getSearchList()
})
onUpdated(() => {
  queryData.queryItem = data.query
  indexStore.setQueryCache(data.query,'set')
})

const getSearchList = async () => {
  indexStore.setPageLoading(true)
  data.query = setTimeValue(data.query)
  let res = await getInstrument(data.query),
    hotGames = [] as any[],
    gamesDetails = {} as any,
    today = {} as any,
    yesterday = {} as any;
  //图标 环比=(今天-昨天)/昨天 * 100% 
  gamesDetails = res.data.operator
  today = res.data.operator.today
  yesterday = res.data.operator.yesterday
  data.operatorList.forEach((item: any) => {
    // 下注次数 
    if(item.key == 'game_count'){
      item.value=today.game_count
      item.radio=BigPercentNumber(BigMinus(today.game_count,yesterday.game_count),yesterday.game_count)
    }
    // 今日RTP 
    if(item.key == 'rtp'){
      item.value=RtpPercent(today.game_win, today.game_bet)
      let todayRadio=BigPercentNumber(today.game_win, today.game_bet)
      let yesterdayRadio=BigPercentNumber(yesterday.game_win, yesterday.game_bet)
      item.radio=BigPercentNumber(BigMinus(todayRadio,yesterdayRadio), yesterdayRadio as number)
    }
    // 今日热门
    if (item.key == 'hot_games' && gamesDetails.hot_games) {
      gamesDetails.hot_games.split(',').forEach((item: string) => {
        hotGames.push(selectToLabel(indexStore.gameArray, item))
      })
      data.operatorList[2].value = hotGames as []
    }
    // 总用户数 
    if(item.key == 'total_person'){
      item.value=today.total_person
      item.radio=BigPercentNumber(BigMinus(today.total_person,yesterday.total_person),yesterday.total_person)
    }
    // 实时人数 
    if(item.key == 'online_person'){
      item.value=today.online_person 
      item.radio=BigPercentNumber(BigMinus(today.online_person,yesterday.online_person),yesterday.online_person)
    }
    // 投注用户 
    if(item.key == 'valid_user'){
      item.value=today.valid_user
      item.radio=BigPercentNumber(BigMinus(today.valid_user,yesterday.valid_user),yesterday.valid_user)
    }
    // 注册用户 
    if(item.key == 'new_user'){
      item.value=today.new_user
      item.radio=BigPercentNumber(BigMinus(today.new_user,yesterday.new_user),yesterday.new_user)
    }
    // 活跃商户 
    if(item.key == 'active_agents'){
      item.value=today.active_agents
      item.radio=BigPercentNumber(BigMinus(today.active_agents,yesterday.active_agents),yesterday.active_agents)
      item.otherValue=gamesDetails.total_agents
    }
    // 今日输赢 
    if(item.key == 'game_income'){
      item.value=BigWan(today.game_income)
      item.radio=BigPercentNumber(BigMinus(today.game_income,yesterday.game_income),yesterday.game_income)
      item.otherValue=BigWan(today.history_game_income)
    }
    // 今日盈利率 
    if(item.key == 'today_earning'){
      item.value=EarningPercent(today.game_bet,today.game_win)+'%'
      item.radio=EarningPercent(today.history_game_bet,today.history_game_win)
      let todayEarning = EarningPercent(today.game_bet, today.game_win),
      yesterdayEarning = EarningPercent(yesterday.history_game_bet, yesterday.history_game_win);
      item.otherValue = EarningPercentRadio(BigMinus(todayEarning, yesterdayEarning), yesterdayEarning)
    }
  })
  //表格
  if (res.data.games) {
    res.data.games?.forEach((item: any) => {
      item.win_percent = BigPercentNumber(item.game_win_person, item.valid_user) // 人数赢百分比 = 赢的人数 / 投注人数
      item.rtp = BigPercentNumber(BigWan(item.game_win), BigWan(item.game_bet)) // RTP = 总赢分 / 总下注
      item.history_rtp = BigPercentNumber(BigWan(item.history_game_win), BigWan(item.history_game_bet)) // 历史RTP = 历史总赢分 / 历史总下注
    })
    data.list = res.data.games
    data.total = res.data.total
  }
}

// 排序
const sortTable = async (val: any) => {
  if(val.order == 'desc'){
    data.list = sortObjectArray(data.list,val.prop,'desc')
  }else{
    data.list = sortObjectArray(data.list,val.prop,'asc')
  }
}

// 详情
const jumpLink = async (name: string, path: string) => {
  router.push({ name: name })
  localStorage.setItem('path',path)
}

// 修改页数
const changeSize = (val: number) => {
  data.query.size = val
}

const changePage = (val: number) => {
  data.query.page = val
  getSearchList()
}

// 重置表头
const resetData = (val: any) => {
  data.fields = val
}

// 重置
const OperatorRef = ref<any>()
const onHideDetails = (show: boolean) => {
  if (show) {
    data.query = {
      page: 1,
      size: 50,
      agent_id: -1,
      operated_id: -1,
      currency_id: -1,
      is_usdt_show: 0,
      date: [getDateDay(0), getDateDay(0)]
    }
    getSearchList()
    OperatorRef.value.onHideDetails()
  }
}
</script>

<style scoped lang="scss">
.box-data {
  // width: 23%;
  width: 18%;
  max-height: 141px;
  padding: 5px 10px;
  margin: 5px 10px;
  // margin: 5px 10px;
  border-radius: 15px;
  font-size: 21px;
  user-select: none; //禁止被用户选中
  border: 1px solid var(--text);

  .box-title {
    color: var(--text);
    font-size: 18px;
  }

  .box-icon {
    width: 120px;
    margin-left: 10px;

    img {
      width: 90px;
      height: 80px;
      border-radius: 5px;
      margin-bottom: -5px;
    }

    .item_gamesDetails {
      width: 50%;
      cursor: pointer;
      color: cornflowerblue;
      border-bottom: 1px solid cornflowerblue;
      font-size: 12px;
      font-weight: 600px;
    }
  }

  .box-value {
    color: cornflowerblue;
    font-weight: 24px;
  }

  .box-yesterday {
    font-size: 14px;
  }

  .colorDark {
    font-size: 14px;
    justify-content: center;
    color: #fcce3e;

    img {
      height: 30px;
    }
  }

  .game_name {
    min-height: 70px;
    font-size: 14px;
    font-weight: 600;
  }
}
</style>
